package it.ipermemory.logic;

import java.io.IOException;
import java.util.ArrayList;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

/**
 * Defines a object that represents the logic game
 * 
 * @author Stefano Tonello (stefano.tonello8821@gmail.com)
 * @version 1.0
 */
public class Game {
	public static final int GAME_NORMAL = 0;
	public static final int GAME_EASIER = 1;
	public static final String GAME_MODE = "MODE";
	public static final String GAME_SCORE = "SCORE";
	public static final String GAME_THING_CARD = "THING_CARD";

	/**
	 * an arrayList of the boxes of the game
	 */
	protected ArrayList<Box> grid;
	/**
	 * The number of attempts
	 */
	protected int attempts;
	/**
	 * The first bpx of the couple selected
	 */
	protected Box firstBox;
	/**
	 * The second bpx of the couple selected
	 */
	protected Box secondBox;
	/**
	 * The score of the game
	 */
	protected int score;
	/**
	 * Number of the couples guessed
	 */
	protected int guessedCouples;
	/**
	 * ArrayList of Thing
	 */
	private final ArrayList<Thing> things;
	/**
	 * The XML parser
	 */
	public static ThingsParserXML parserXML = null;

	public Game(XmlPullParser xpp) throws XmlPullParserException, IOException {
		parserXML = ThingsParserXML.parseXml(xpp);
		things = parserXML.getAllThings();

		attempts = 0;
		guessedCouples = 0;

		grid = new ArrayList<Box>();

		for (int i = 0; i < things.size(); i++) {
			grid.add(new Box(things.get(i).getId()));
			grid.add(new Box(things.get(i).getId()));
			grid.get(i).setBrother(grid.get((i) + 1));
			grid.get((i) + 1).setBrother(grid.get(i));
		}

		firstBox = null;
		secondBox = null;
	}

	public int getAttempts() {
		return attempts;
	}

	public void setAttempts(int attemptsTemp) {
		attempts = attemptsTemp;
	}

	public Box getFirstBox() {
		return firstBox;
	}

	public void setFirstBox(Box firstBox) {
		this.firstBox = firstBox;
	}

	public Box getSecondBox() {
		return secondBox;
	}

	public void setSecondBox(Box secondBox) {
		this.secondBox = secondBox;
	}

	public int getScore() {
		return score;
	}

	public void setScore(int score) {
		this.score = score;
	}

	public int getGuessedCouples() {
		return guessedCouples;
	}

	public void setGuessedCouples(int guessedCouples) {
		this.guessedCouples = guessedCouples;
	}

	/**
	 * it calculates the score for the normal game
	 * 
	 * @param elapsedSeconds
	 *            time elapsed from the start of the game
	 * @return the score calculated
	 */
	public int calculateScore(int elapsedSeconds) {
		score = score - (10 * attempts) - elapsedSeconds;
		if (score < 0) {
			score = 0;
		}
		return score;
	}

	/**
	 * it calculates the score for the easy game
	 * 
	 * @param elapsedSeconds
	 *            time elapsed from the start of the game
	 * @param numberHelpVisual
	 *            number of the visual helps used
	 * @param numberHelpAudio
	 *            number of the audio helps used
	 * @return the score calculated
	 */
	public int calculateScore(int elapsedSeconds, int numberHelpVisual,
			int numberHelpAudio) {
		score = score - (10 * attempts) - elapsedSeconds
				- (5 * numberHelpVisual) - (5 * numberHelpAudio);

		if (score < 0) {
			score = 0;
		}
		return score;
	}

	public ArrayList<Box> getGrid() {
		return grid;
	}

	public ThingsParserXML getParserXML() {
		return parserXML;
	}
}
